﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Systemathics.FrameWork;
using Systemathics.UserControls.Chart.Properties;

namespace Systemathics.UserControls
{
    public sealed partial class HistoricalChartForm : Form
    {
        private readonly Object serieToDraw;
        private readonly Series Chartserie;

        public HistoricalChartForm(Object serie)
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            DoubleBuffered = true;

            Icon = Icon.FromHandle(Resources.Hors_ligne.GetHicon());
            SizeGripStyle = SizeGripStyle.Hide;
            MaximizeBox = true;
            MinimizeBox = true;
            StartPosition = FormStartPosition.CenterScreen;

            FormBorderStyle = FormBorderStyle.Sizable;
            ControlBox = true;
            Opacity = 0.9;

            serieToDraw = serie; 
            
            Chartserie = new Series
                            {
                                ChartType = SeriesChartType.FastLine,
                                BorderWidth = 1,
                                Color = Color.Black,
                                XValueType = ChartValueType.Time,
                                IsVisibleInLegend = false
                            };

            chart.ChartAreas[0].Position.Auto = false;
            chart.ChartAreas[0].Position.X = 1;
            chart.ChartAreas[0].Position.Y = 1;
            chart.ChartAreas[0].Position.Width = 90;
            chart.ChartAreas[0].Position.Height = 90;

            chart.ChartAreas[0].AxisX.IsStartedFromZero = false;
            chart.ChartAreas[0].AxisX.IsLabelAutoFit = true;
            chart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.WhiteSmoke;
            chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            chart.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            chart.ChartAreas[0].AxisX.ScrollBar.Size = 15;

            chart.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chart.ChartAreas[0].AxisY.LabelAutoFitMinFontSize = 10;
            chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
            chart.ChartAreas[0].AxisY.IsLabelAutoFit = true;
            chart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.WhiteSmoke;
            chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

            chart.ChartAreas[0].CursorX.AutoScroll = true;
            chart.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;

            chart.ChartAreas[0].CursorY.AutoScroll = true;
            chart.ChartAreas[0].CursorY.IsUserEnabled = true;
            chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

            chart.Series[0].IsXValueIndexed = true;

            chart.Series.Add(Chartserie);
            chart.Legends.Clear();
            chart.EndInit();
            chart.Invalidate();
        }

        private double previousTime;
        private void HistoricalChartForm_Load(object sender, EventArgs e)
        {
            if (serieToDraw is TimeSeries)
            {
                var ts = (serieToDraw as TimeSeries);
                var minTime = ts.TimeSerie.FirstOrDefault().Key.ToOADate();
                var maxTime = ts.TimeSerie.FirstOrDefault().Key.ToOADate();
                Text =  string.Format("{0} elements - From :{1} To : {2}", ts.Count(), minTime, maxTime);
                previousTime = 0.0;
                foreach (var series in ts.TimeSerie)
                {
                    var time = series.Key.ToOADate();
                    if(time > previousTime)
                    {
                        Chartserie.Points.Add(new DataPoint(time, (double)series.Value));
                        previousTime = time;
                    } 
                }
            }
            else if (serieToDraw is QuoteSeries)
            {
                var qs = (serieToDraw as QuoteSeries);
                Text = string.Format("{0} - {1} Quotes - From :{2} To : {3}", qs.Ticker, qs.Count, qs.FirstOrDefault().Time, qs.LastOrDefault().Time);
                previousTime = 0.0;
                foreach (Quote quote in qs)
                {
                    var time = quote.Time.ToOADate();
                    if (time > previousTime)
                    {
                        Chartserie.Points.Add(new DataPoint(time, Decimal.ToDouble(quote.Mid)));
                        previousTime = time;
                    }
                }
            }
            else if (serieToDraw is TradeSeries)
            {
                var t = (serieToDraw as TradeSeries);
                Text = string.Format("{0} - {1} Trades - From :{2} To : {3}", t.Ticker, t.Count, t.FirstOrDefault().Time, t.LastOrDefault().Time);
                previousTime = 0.0;
                foreach (Trade trade in t)
                {
                    var time = trade.Time.ToOADate();
                    if (time > previousTime)
                    {
                        Chartserie.Points.Add(new DataPoint(time, Decimal.ToDouble(trade.Price)));
                        previousTime = time;
                    }
                }
            }

            //chart.ChartAreas[0].AxisX.MajorGrid.Interval = 1;
            //chart.ChartAreas[0].AxisX.MajorTickMark.Interval = 1;
            //chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
            chart.ChartAreas[0].AxisX.Interval = 40;
            chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
            chart.ChartAreas[0].AxisX.IntervalOffset = 5;
            chart.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Minutes;

        }
    }
}
